package com.chenxin.wms.process.check.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.enums.WmsTaskStatus;
import com.chenxin.common.enums.WmsTaskSubType;
import com.chenxin.common.enums.WmsTaskType;
import com.chenxin.common.enums.wms.LocationType;
import com.chenxin.common.enums.wms.StockStatus;
import com.chenxin.common.utils.StringUtils;
import com.chenxin.wms.domain.StockEntity;
import com.chenxin.wms.domain.TaskEntity;
import com.chenxin.wms.domain.dto.StockCheckCreateParams;
import com.chenxin.wms.domain.po.LocationInfo;
import com.chenxin.wms.mapper.LocationMapper;
import com.chenxin.wms.mapper.StockMapper;
import com.chenxin.wms.mapper.TaskMapper;
import com.chenxin.wms.process.check.IProcStockCheckCreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class ProcStockCheckCreateImpl implements IProcStockCheckCreate {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private LocationMapper locationMapper;

    @Override
    public ProcResult call(StockCheckCreateParams params) {
        if (params == null) {
            return ProcResult.error("check_create_error001", "参数不能为空");
        }
        // 获取盘点参数
        Long stockId = params.getStockId();
        String stationNo = params.getStationNo();
        String checkFlag = params.getCheckFlag();
        String comments = params.getComments();
        Long creator = params.getCreator();
        Date workDate = new Date();
        // 盘点库存不能为空
        if (StringUtils.isNull(stockId)) {
            return ProcResult.error("check_create_error002", "请选择需要盘点的库存！");
        }
        // 盘点站台根据现场情况可选

        // 根据库存ID获取对应库存(非在库库存不允许盘点)
        StockEntity checkStock = stockMapper.selectById(stockId);
        if (checkStock == null) {
            return ProcResult.error("check_create_error003", "所选库存【" + stockId + "】不存在！");
        }
        // 获取目标库存相关信息（托盘、位置、物料、数量、状态等信息）
        String contNo = checkStock.getContNo();
        String locationNo = checkStock.getLocationNo();
        String spuNo = checkStock.getSpuNo();
        BigDecimal stockQty = checkStock.getStockQty();
        int stockStatus = checkStock.getStockStatus();
        // 非初始状态的库存不允许盘点（可能已用于备货等场景）
        if (stockStatus > StockStatus.USABLE.getCode()) {
            return ProcResult.error("check_create_error004", "所选库存【" + stockId + "】的状态非【空闲】不允许盘点！");
        }
        // 根据所在位置获取信息
        LocationInfo locationByStock = locationMapper.selectLocationByNo(locationNo);
        if (locationByStock == null) {
            return ProcResult.error("check_create_error005", "所选库存【" + stockId + "】的所在位置【" + locationNo + "】未在货位表配置！");
        }
        String locationType = locationByStock.getLocationType();
        if (!LocationType.HW.getCode().equals(locationType)) {
            return ProcResult.error("check_create_error006", "所选库存【" + stockId + "】的所在位置【" + locationNo + "】非货位，不允许盘点！");
        }
        // 是否存在其他业务场景的内容
        QueryWrapper<TaskEntity> existsTaskQW = new QueryWrapper<>();
        existsTaskQW.lambda().eq(TaskEntity::getFromContNo, contNo)
                .ne(TaskEntity::getTaskType, WmsTaskType.CHECK)
                .lt(TaskEntity::getTaskStatus, WmsTaskStatus.COMPLETE.getCode());
        TaskEntity existsTask = taskMapper.selectOne(existsTaskQW);
        if (existsTask != null) {
            return ProcResult.error("check_create_error007", "所选盘点托盘【" + contNo + "】存在其他业务场景未完成的任务，不允许盘点！");
        }
        // 锁定库存为盘点
        checkStock.setStockStatus(StockStatus.ALLOCATED.getCode());
        checkStock.setLockedQty(stockQty);
        checkStock.setUpdater(creator);
        checkStock.setUpdateDate(workDate);
        checkStock.setComments("当前库存盘点中！");
        stockMapper.updateById(checkStock);
        // 生成盘点任务
        TaskEntity checkTask = new TaskEntity();
        checkTask.setTaskType(WmsTaskType.CHECK.getCode());
        checkTask.setTaskSubType(WmsTaskSubType.CHECK_OUT.getCode());
        checkTask.setFromContNo(contNo);
        checkTask.setToContNo(contNo);
        checkTask.setFromPosition(locationNo);
        checkTask.setToPosition("目标盘点位置");
        checkTask.setCurPosition(locationNo);
        checkTask.setStockId(stockId);
        checkTask.setSpuNo(spuNo);
        checkTask.setPlanQty(stockQty);
        checkTask.setLotNo(checkStock.getLotNo());
        checkTask.setReelId(checkStock.getReelId());
        checkTask.setStationNo("目标盘点站台");
        checkTask.setEbeln(checkStock.getEbeln());
        checkTask.setEbelp(checkStock.getEbelp());
        checkTask.setCreator(creator);
        checkTask.setCreateDate(workDate);
        checkTask.setComments(comments);
        taskMapper.insert(checkTask);
        return ProcResult.success("所选库存【" + stockId + "】盘点任务创建成功！任务ID【" + checkTask.getTaskId() + "】,托盘条码【" + contNo + "】");
    }
}
